#include "KeyboardStateFilter.h"
#include <QKeyEvent>
#include <QDebug>

bool KeyboardStateFilter::getKey(Qt::Key k) const
{
  return mKeys.value(k, false);
}

bool KeyboardStateFilter::eventFilter(QObject *object, QEvent *event)
{
  QKeyEvent* e(dynamic_cast<QKeyEvent*>(event));
  if (e)
  {
    Qt::Key k(static_cast<Qt::Key>(e->key()));
    if (e->type() == QEvent::KeyPress)
    {
      mKeys[k] = true;
      emit keyPress(k);
    }
    else if (e->type() == QEvent::KeyRelease)
    {
      mKeys[k] = false;
      emit keyRelease(k);
    }
  }

  return QObject::eventFilter(object, event);
}
